package com.kkb.controller;

import com.kkb.pojo.User;
import com.kkb.service.LoginService;
import com.kkb.vo.ResultVO;
import com.kkb.vo.SessionVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("login")
@ResponseBody
public class LoginController {
    @Resource
    private LoginService loginService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultVO<Integer> login(@RequestParam("username") Integer u_loginName,
                                @RequestParam("password") String u_passWord,
                                @RequestParam("yzm1") String yzm1,
                                @RequestParam("yzm2") String yzm2,
                                HttpServletRequest request) {
        User user = loginService.queryById(u_loginName);
        if (yzm1 != null && yzm1.equals(yzm2)){
            if (user != null){
                if (user.getU_passWord().equals(u_passWord) && user.getU_state() == 0){
                    SessionVO sessionVO = new SessionVO();
                    sessionVO.setU_loginName(u_loginName);
                    sessionVO.setU_passWord(u_passWord);
                    request.getSession().setAttribute("sessionVO",sessionVO);
                    System.out.println("sessionVO="+sessionVO);
                    return new ResultVO<>();
                }
                if (user.getU_state() == 1){
                    return new ResultVO<>(-3,"账号被禁用");
                }
                return new ResultVO<>(-2,"密码错误");
            }else {
                return new ResultVO<>(-1,"账号不存在");
            }
        }else {
            return new ResultVO<>(-2,"验证码不正确");
        }
    }
    //获取登录名
    @RequestMapping(value = "getName",method = RequestMethod.GET)
    public ResultVO<User> getName(HttpServletRequest request){
        SessionVO sessionVO = (SessionVO) request.getSession().getAttribute("sessionVO");
        User user = loginService.queryById(sessionVO.getU_loginName());
        if (user != null){
            return new ResultVO<>(user);
        }
        return new ResultVO<>(404,"数据丢失");
    }

    //修改密码
    @RequestMapping(value = "update",method = RequestMethod.PUT)
    public ResultVO<Integer> update(String u_passWord,String newPsw
                                    ,String confirmPsw,HttpServletRequest request){
        SessionVO sessionVO = (SessionVO) request.getSession().getAttribute("sessionVO");
        if (u_passWord!=null && u_passWord.equals(sessionVO.getU_passWord())){
            if (newPsw != null && newPsw.equals(confirmPsw)){
                User user = new User();
                user.setU_loginName(sessionVO.getU_loginName());
                user.setU_passWord(newPsw);
                Integer update = loginService.update(user);
                if (update>0){
                    return new ResultVO<>(200,"修改成功,请重新登录");
                }else {
                    return new ResultVO<>(500,"修改失败");
                }
            }else {
                return new ResultVO<>(500,"两次输入的密码不一致");
            }
        }else {
            return new ResultVO<>(500,"密码不正确");
        }
    }
    //退出
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public ResultVO<String> logout(HttpServletRequest request){
        request.getSession().invalidate();
        return new ResultVO<>();
    }
}
